package com.gzsxy.esjy.sign.service.impl;

import com.alibaba.fastjson.JSON;

import com.gzsxy.esjy.sign.SignConstant;
import com.gzsxy.esjy.sign.async.AsyncWsClient;
import com.gzsxy.esjy.sign.cache.SignCacheManager;
import com.gzsxy.esjy.sign.dto.AndroidSignProtocolDto;
import com.gzsxy.esjy.sign.dto.BaseSignProtocolDto;
import com.gzsxy.esjy.sign.dto.SignTaskDto;
import com.gzsxy.esjy.sign.exception.SignException;
import com.gzsxy.esjy.sign.service.BaseSignProtocol;
import com.gzsxy.esjy.sign.service.GSWsChannelService;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 安卓签字版
 * @author zhangxiaolong
 * @since 2022/7/15
 */
@Slf4j
@Service(SignConstant.ANDROID)
public class AndroidSignProtocolImpl implements BaseSignProtocol {

    @Autowired
    private GSWsChannelService gsWsChannelService;

    @Autowired
    private SignCacheManager cacheManager;

    /**
     * 进行签名
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void sign(String ip, Integer port, BaseSignProtocolDto dto) {
        if (dto instanceof AndroidSignProtocolDto) {
            AndroidSignProtocolDto protocolDto = (AndroidSignProtocolDto) dto;
            //判断当前指定是否正确
            if (!protocolDto.getMsgDto().getCmd().equals(SignConstant.P_SIGN)) {
                throw new SignException("指令参数错误：" + protocolDto.getMsgDto().getCmd());
            }
        }
        //判断缓存中是否存在当前签名任务
        if (cacheManager.getSignTaskCache().containsKey(dto.getRequestId())) {
            //throw new SignException(InnerSignCode.SIGN_ING_ERROR_CODE, "当前文件正在签名中");
            throw new SignException("当前文件正在签名中");
        }

        AsyncWsClient connect = gsWsChannelService.connect(ip, port, dto);
        try {
            Thread.sleep(500);
        } catch (InterruptedException ignored) {
        }

        //添加任务缓存
        SignTaskDto signTaskDto = new SignTaskDto();
        signTaskDto.setIp(ip);
        signTaskDto.setPort(port);
        signTaskDto.setRequestId(connect.getRequestId());
        signTaskDto.setDevType(dto.getDevType());
        signTaskDto.setDevTypeName(dto.getDevTypeName());
        signTaskDto.setTaskId(connect.getTaskId());
        signTaskDto.setRequestTime(new Date());

        //当前任务存入缓存
        cacheManager.getSignTaskCache().put(dto.getRequestId(), signTaskDto);
        log.info("sign 【{}】",JSON.toJSONString(cacheManager));
        connect.getChannel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(dto)));
    }

    /**
     * 取消签名
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void cancelSign(String ip, Integer port, BaseSignProtocolDto dto) {
        log.info("cancelSign 【{}】",JSON.toJSONString(cacheManager));
        SignTaskDto signTaskDto = cacheManager.getSignTaskCache().get(dto.getRequestId());
        if (null == signTaskDto) {
            log.warn("签名任务不存在:{}", JSON.toJSONString(dto));
            return;
        }

        cacheManager.clearSignByRequestId(dto.getRequestId(), true, false);
    }

    /**
     * 设置设备信息
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void setDeviceInfo(String ip, Integer port, BaseSignProtocolDto dto) {

    }

    /**
     * 获取签字板设备状态
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param type
     */
    @Override
    public void getDeviceStatus(String ip, Integer port, Integer type) {

    }

    /**
     * 设置签字板属性
     *
     * @param ip   设备IP
     * @param port 设备端口
     * @param dto  协议实体
     */
    @Override
    public void setSignDevParam(String ip, Integer port, BaseSignProtocolDto dto) {

    }
}
